{extend name="/main_not_nav"}
{block name="body"}
<br>
<form class="layui-form">
    {:token_field()}
    <div class="layui-form-item">
        <label class="layui-form-label">ID</label>
        <div class="layui-input-inline">
            <input type="number" name="id" value="{$data.id}" readonly="true" lay-verify="required" autocomplete="off" placeholder="请输入内容" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">上级菜单</label>
        <div class="layui-input-inline">
            <select name="pid" id="pid">
                <option value="0">顶级菜单</option>
                {foreach $pid as $val}
                <option value="{$val.id}">{$val.title}</option>
                {/foreach}
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">名称</label>
        <div class="layui-input-inline">
            <input type="text" name="title" value="{$data.title}" lay-verify="required" autocomplete="off" placeholder="请输入内容" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">链接</label>
        <div class="layui-input-inline">
            <input type="text" name="uri" value="{$data.uri}" lay-verify="required" autocomplete="off" placeholder="顶级菜单请输入 #" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">排序</label>
        <div class="layui-input-inline">
            <input type="number" name="rank" value="{$data.rank}" lay-verify="required" autocomplete="off" placeholder="请输入数字" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">显示</label>
        <div class="layui-input-inline">
            <select name="is_show" id="is_show">
                {foreach $is_show as $key => $val}
                <option value="{$key}">{$val}</option>
                {/foreach}
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="formDemo">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
{/block}
{block name="script"}
<script>
    layui.use(['form', 'laydate'], function(){
        var form = layui.form;
        let pid_value = "{$data.pid}";
        $("#pid").find("option[value='"+pid_value+"']").attr('selected', true);
        let is_show_value = "{$data.is_show}";
        $("#is_show").find("option[value='"+is_show_value+"']").attr('selected', true);
        form.render('select');

        //监听提交
        form.on('submit(formDemo)', function(data){
            //当前容器的全部表单字段，名值对形式：{name: value}
            // console.log(data.field);
            // layer.msg(JSON.stringify(data.field));
            $.ajax({
                url: "{$uri}/update",
                method: "put",
                data: data.field,
                dataType: "JSON",
                success: function (callback){
                    // console.log(callback);
                    layer.msg(callback.message);
                    if (callback.code === 1)
                    {
                        setTimeout(function(){
                            window.parent.location.reload();
                        }, 1000);
                    }
                }
            });
            return false;
        });
    });
</script>
{/block}